<template>
  <el-select v-model="value" multiple placeholder="请选择" @change="handleChange">

    <el-option v-for="item in option" :key="item" :label="item" :value="item">
    </el-option>
  </el-select>


</template>

<script>
export default {
  props: ["options", "value1"],
  data() {
    return {
      value: [this.value1],
      option: JSON.parse(JSON.stringify(this.options || [])),
      selectVal: []
    }
  },
  created() {
    this.init()
  },
  watch: {
    option: {
      immediate: true,
      deep: true,
      handler(n) {
        this.value = [this.value1 || n[0]]
        if (this.value.includes("全部")) {
          this.value = ["全部"]
          this.selectVal = n.filter(item => item !== '全部')
        }
      }
    }
  },
  methods: {
    init() {
      this.option.unshift("全部")
    },
    handleChange(n) {
      // 上一次的值 是 全部
      if (n.includes("全部")) {
        this.value = ["全部"]
        this.selectVal = this.option.filter(item => item !== '全部')
      }
      else {
        this.selectVal = n
      }
      this.$emit("selected", this.selectVal)
    }
  }
}
</script>
